<?php

	include_once "functions.php";
	include "mails_views.php";

	function validateUser(){
		$user = UsersORM::getUser();
		if(isset($user["id"])){
			return true;
		}	
		return false;
	}


	function getSendedTimeMails($user){
			global $wpdb;
			if( invalidUser()){
				return false;
			}			
			$prefix = "timetable_";
			$userid = $user["id"];
			$query = "SELECT idmail mail_id, sender_userid, recepient_userid, 	mail_title, mail_body, mail_new, mail_deleted FROM {$prefix}mails  WHERE sender_userid = $userid";
			$arr = $wpdb->get_results($query, ARRAY_A);
			return $arr;
	}

	function getInboxTimeMails($user){
			global $wpdb;
			if( invalidUser()){
				return false;
			}			
			$prefix = "timetable_";
			$userid = $user["id"];
			$query = "SELECT idmail mail_id, sender_userid, recepient_userid, 	mail_title, mail_body, mail_new, mail_deleted FROM {$prefix}mails  WHERE recepient_userid = $userid";
			$arr = $wpdb->get_results($query, ARRAY_A);
			return $arr;
	}

	function getTimeMailInbox($user, $mail_id){
			global $wpdb;
			if( invalidUser()){
				return false;
			}			
			$prefix = "timetable_";
			$userid = $user["id"];
			$query = "SELECT idmail mail_id, sender_userid, recepient_userid, 	mail_title, mail_body, mail_new, mail_deleted FROM {$prefix}mails  WHERE recepient_userid = $userid AND idmail = $mail_id";
			$arr = $wpdb->get_results($query, ARRAY_A);
			//var_dump($arr);
			if(count($arr)==0){
				$res = "Письмо не найдено!";
			}else{
				$res = $arr[0]["mail_body"];
			}
			return $res;
	}

	function sendTimeMail($data){
		global $wpdb;
		$prefix = "timetable_";
	
		if( invalidUser()){
			return false;
		}
			
		$user = UsersORM::getUser();

		$sender_userid  = $user['id'];
		$recepient_userid  = $data["recepient_userid"];
		$mail_title  = $data['mail_title'];
		$mail_body  = $data['mail_body'];

		return $wpdb->insert(
			$prefix."mails",
			array(
					"sender_userid"    => $sender_userid,
					"recepient_userid" => $recepient_userid,
					"mail_title"       => $mail_title,
					"mail_body"        => $mail_body,
					"mail_new"		   => 1,
					"mail_deleted"	   => 0

				),
			array(
					'%d',
					'%d',
					'%s',
					'%s',
					'%d',
					'%d'
				)
		);

		
		/*$query = $wpdb->prepare(
						"INSERT INTO {$prefix}mails 
						(
							sender_userid,
							recepient_userid,
							mail_title,
							mail_body,
							mail_new,
							mail_deleted
						)
						VALUES	
						(
							%d,
							%d,
							%s,
							%s,
							%d,
							%d
						)",
						$sender_userid, $recepient_userid, $mail_title, $mail_body, 1, 0		
				);
		
		

		return $wpdb->query($query);*/

	}	


	function setReaded($data){
		if(invalidUser()){
			return false;
		}

		global $wpdb;
		$prefix = "timetable_";
		$wpdb->update(
			$prefix."mails",
			array(
					"mail_new"=>0
				),
			array(
					"mail_id" => $data["mail_id"]
				),
			array(
					"%d"
				)
		);

	}

?>